package com.southwind.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.southwind.common.ResultObj;
import com.southwind.entity.ClassOrder;
import com.southwind.entity.ClassTable;
import com.southwind.entity.Member;
import com.southwind.form.OrderForm;
import com.southwind.service.ClassOrderService;
import com.southwind.service.ClassTableService;
import com.southwind.service.MemberService;
import com.southwind.vo.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2024-12-17
 */
@RestController
@RequestMapping("/order")
public class ClassOrderController {

    @Autowired
    private ClassOrderService orderService;
    @Autowired
    private ClassTableService classService;
    @Autowired
    private MemberService memberService;

    @GetMapping("/list")
    public PageVO list(String name,Integer page,Integer limit){
        Page<ClassOrder> pageModel = new Page<>(page, limit);
        QueryWrapper<ClassOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotBlank(name), "member_name", name);
        Page<ClassOrder> resultPage = this.orderService.page(pageModel,queryWrapper);
        PageVO pageVO = new PageVO();
        pageVO.setData(resultPage.getRecords());
        pageVO.setCount(resultPage.getTotal());
        return pageVO;
    }

    @GetMapping("/getData")
    public Map getData(){
        List<ClassTable> classTableList = this.classService.list();
        List<Member> memberList = this.memberService.list();
        Map<String,Object> map = new HashMap<>();
        map.put("classList", classTableList);
        map.put("memberList", memberList);
        return map;
    }

    @PostMapping("/add")
    public ResultObj add(@RequestBody OrderForm orderForm){
        ClassTable classTable = this.classService.getById(orderForm.getCid());
        Member member = this.memberService.getById(orderForm.getMid());
        ClassOrder classOrder = new ClassOrder();
        classOrder.setClassId(classTable.getClassId());
        classOrder.setClassBegin(orderForm.getDate());
        classOrder.setClassName(classTable.getClassName());
        classOrder.setCoach(classTable.getCoach());
        classOrder.setMemberAccount(member.getMemberAccount());
        classOrder.setMemberName(member.getMemberName());
        boolean save = this.orderService.save(classOrder);
        if(save) return ResultObj.ORDER_ADD_SUCCESS;
        return ResultObj.ORDER_ADD_ERROR;
    }

}

